package view;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.util.Observable;
import java.util.Observer;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

import model.Chess;
import model.ChessAraay;
import model.Prompt;
import util.SocketUtil;

public class GridPanel extends JPanel implements Observer{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int chessw,chessh,left,top;
	private Prompt prompt;
	private ChessAraay chessAraay;
	private Image blackChess,whiteChess;
	
	public GridPanel() {
		super();		
		setBackground(new Color(0xB2DFEE));
		setOpaque(false);
		prompt=new Prompt();
		prompt.setPrompt("请点击开始", 48, Color.black, 1f);
		addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				super.mouseClicked(e);
				if(chessAraay.isMy()==false) return; 
				int j=(e.getX()-left)/chessw;
				int i=(e.getY()-top)/chessh;
				Chess chess=new Chess(); chess.setX(j); chess.setY(i); chess.setColor(SocketUtil.getColor());
				if(chessAraay.saveChess(chess)) 
				{
					SocketUtil.sendMessage(chess);
					repaint();
				}
			}
		});
		
		blackChess=new ImageIcon(getClass().getResource("../image/blackChessman.png")).getImage();
		whiteChess=new ImageIcon(getClass().getResource("../image/whiteChessman.png")).getImage();
	}
	
	public GridPanel(ChessAraay chessAraay) {
		this();
		this.chessAraay = chessAraay;
	}

	@Override
	public void paint(Graphics g) {
		// TODO Auto-generated method stub
		super.paintComponent(g);
		Graphics2D g2=(Graphics2D) g;		
		Composite composite=g2.getComposite();
		g2.setColor(new Color(0xB2DFEE));
		g2.setComposite(AlphaComposite.SrcOver.derive(0.8f));
		g2.fillRect(0, 0, getWidth(), getHeight());
		g2.setComposite(composite);
		drawGrid(g2);	
		drawChess(g2);
		drawPrompt(g2, prompt);
	}
	
	private void drawChess(Graphics2D g){
		for(int i=0;i<ChessAraay.ROWS;i++)
			for(int j=0;j<ChessAraay.COLUMNS;j++){
				if(chessAraay.getChess(i, j)!=null){
					Chess chess=chessAraay.getChess(i, j);
					if(chess.equalColor(Color.black))
						g.drawImage(blackChess, chess.getX()*chessw+left, chess.getY()*chessh+top, chessw, chessh, this);
					else
						g.drawImage(whiteChess, chess.getX()*chessw+left, chess.getY()*chessh+top, chessw, chessh, this);
				}
			}
	}
	
	private void drawPrompt(Graphics2D g,Prompt prompt){
		Composite composite=g.getComposite();
		g.setColor(prompt.getColor());
		g.setComposite(AlphaComposite.SrcOver.derive(prompt.getAlpha()));
		Font font=new Font("隶书", Font.TRUETYPE_FONT, prompt.getFontsize());
		g.setFont(font);
		FontRenderContext frc=g.getFontRenderContext();
		Rectangle2D rt=font.getStringBounds(prompt.getMessage(), frc);
		g.drawString(prompt.getMessage(),(int)(getWidth()-rt.getWidth())/2, (int)(getHeight()-rt.getHeight())/2);
		g.setComposite(composite);
	}
	
	private void drawGrid(Graphics2D g2) {
		int width=getWidth();
		int height=getHeight();
		chessw=width/15;
		chessh=height/15;
		left=width%15/2;
		top=height%15/2;
		int x=left+chessw/2,y=top+chessh/2;
		g2.setColor(Color.black);
		for(int i=0;i<15;i++){
			g2.drawLine(x+i*chessw, y, x+i*chessw,height-y );
			g2.drawLine(x, y+i*chessh, width-x, y+i*chessh);
		}
	}
	
	public Prompt getPrompt(){
		return prompt;
	}

	@Override
	public void update(Observable o, Object arg) {
		if(arg instanceof Chess){			
			Chess chess=(Chess) arg;
			chessAraay.saveChess(chess);
			repaint();			
		}
	}

}
